import * as dotEnv from 'dotenv';
// 静态的配置
export default () => ({
  LOG_PATH: process.env.DATABASE_HOST || './logs',
  LOG_LEVEL: process.env.LOG_LEVEL || 'debug',
  REDIS_HOST: process.env.REDIS_HOST || 'localhst',
  REDIS_PORT: process.env.REDIS_PORT || 6379,
});
dotEnv.config();
console.log(dotEnv.config());
/**
 * 格式化环境变量
 * @param key 环境变量的键值
 * @param defaultValue 默认值
 * @param callback 格式化函数
 */
const formatValue = <T>(
  key: string,
  defaultValue: T,
  callback: (value: string) => T,
): T => {
  const value: string | undefined = process.env[key];
  if (typeof value === 'undefined') {
    return defaultValue;
  }
  return callback(value);
};

export const env = (key: string, defaultValue = '') =>
  formatValue(key, defaultValue, (value) => value);
export const envString = (key: string, defaultValue = '') =>
  formatValue(key, defaultValue, (value) => value);
export const envNumber = (key: string, defaultValue = 0) =>
  formatValue(key, defaultValue, (value) => Number(value));
export const envBoolean = (key: string, defaultValue = false) =>
  formatValue(key, defaultValue, (value) => Boolean(value));
